<?php
class ServerUtil {
  public static function getPageUrl() {
    $pageURL = 'http';
    if (isset($_SERVER['https']) && $_SERVER['https'] == 'on') {
      $pageURL .= 's';
    }
    $pageURL .= '://';
    if ($_SERVER['SERVER_PORT'] != '80') {
      $pageURL .= $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
    } else {
      $pageURL .= $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
    }
    return $pageURL;
  }

  public static function setMetaTitle($title) {
    sfContext::getInstance()->getResponse()->setTitle($title);
  }

  public static function getServerUrl() {

    $serverURL = 'http';
    if (isset($_SERVER['https']) && $_SERVER['https'] == 'on') {
      $serverURL .= 's';
    }
    $serverURL .= '://';
    if ($_SERVER['SERVER_PORT'] != '80') {
      $serverURL .= $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'];
    } else {
      $serverURL .= $_SERVER['SERVER_NAME'];
    }
    return $serverURL;
  }

  public static function getUserBrowser() {
    $uAgent = $_SERVER['HTTP_USER_AGENT'];
    $bname = 'Unknown';
    $version= '';

    if(preg_match('/MSIE/i',$uAgent) && !preg_match('/Opera/i',$uAgent)) {
      $bname = 'Internet Explorer';
      $ub = 'MSIE';
    } elseif(preg_match('/Firefox/i',$uAgent)) {
      $bname = 'Mozilla Firefox';
      $ub = 'Firefox';
    } elseif(preg_match('/Chrome/i',$uAgent)) {
      $bname = 'Google Chrome';
      $ub = 'Chrome';
    } elseif(preg_match('/Safari/i',$uAgent)) {
      $bname = 'Apple Safari';
      $ub = 'Safari';
    } elseif(preg_match('/Opera/i',$uAgent)) {
      $bname = 'Opera';
      $ub = 'Opera';
    } elseif(preg_match('/Netscape/i',$uAgent)) {
      $bname = 'Netscape';
      $ub = 'Netscape';
    }

    $known = array('Version', $ub, 'other');
    $pattern = '#(?<browser>' . join('|', $known) .')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
    if (!preg_match_all($pattern, $uAgent, $matches)) {

    }

    $i = count($matches['browser']);
    if ($i != 1) {
      if (strripos($uAgent,'Version') < strripos($uAgent,$ub)) {
        $version = $matches['version'][0];
      } else {
        $version = $matches['version'][1];
      }
    } else {
      $version= $matches['version'][0];
    }

    if ($version==null || $version=='') {
      $version = '';
    }

    return $bname . ' ' . $version;
  }

  public static function getUserOs() {
    $oses = array (
            'iPhone' => '(iPhone)',
            'Windows 3.11' => 'Win16',
            'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
            'Windows 98' => '(Windows 98)|(Win98)',
            'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
            'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
            'Windows 2003' => '(Windows NT 5.2)',
            'Windows Vista' => '(Windows NT 6.0)|(Windows Vista)',
            'Windows 7' => '(Windows NT 6.1)|(Windows 7)',
            'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
            'Windows ME' => 'Windows ME',
            'Open BSD'=>'OpenBSD',
            'Sun OS'=>'SunOS',
            'Linux'=>'(Linux)|(X11)',
            'Safari' => '(Safari)',
            'Macintosh'=>'(Mac_PowerPC)|(Macintosh)',
            'QNX'=>'QNX',
            'BeOS'=>'BeOS',
            'OS/2'=>'OS/2',
            'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp/cat)|(msnbot)|(ia_archiver)'
    );

    $userAgent = $_SERVER['HTTP_USER_AGENT'];

    foreach($oses as $os => $pattern) {
      if(eregi($pattern, $userAgent)) {
        return $os;
      }
    }
    return 'Unknown';
  }

  public static function getUserIp()
  {
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
      $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
      $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
      $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
  }
}